<template>
  <el-card>
    <el-table border style="margin: 10px 0" :data="skuArr">
      <el-table-column
        label="序号"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="名称"
        align="center"
        show-overflow-tooltip
        width="150px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        align="center"
        show-overflow-tooltip
        width="150px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="图片" align="center" width="150px">
        <template #="{ row, $index }">
          <img :src="row.skuDefaultImg" alt="" style="" />
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        align="center"
        width="150px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格"
        align="center"
        width="150px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="250px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            title="上架/下架"
            @click="updateSale(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            title="编辑"
            @click="updateSku"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            title="查看已有商品详情"
            @click="findSku(row)"
          ></el-button>
          <!-- 删除气泡确认框在table内部，直接在pop上绑定confirm事件发送请求，button不绑定事件 -->
          <el-popconfirm
            @confirm="removeSku(row.id)"
            :title="`你确定要删除${row.skuName}吗？`"
          >
            <el-button
              type="primary"
              size="small"
              icon="Delete"
              title="删除"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @current-change="getHasSku"
      @size-change="handler"
      v-model:current-page="pageNo"
      :page-sizes="[10, 20, 30, 40]"
      v-model:page-size="pageSize"
      layout=" prev, pager, next, jumper,->,sizes,total "
      :total="total"
      :background-color="true"
    ></el-pagination>
    <!-- 抽屉组件展示商品的详情-->
    <el-drawer
      title="查看商品详情"
      v-model="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <template #default>
        <!-- 使用栅格系统布局，一行有24格，:span可设置每一列占用多少格 -->
        <el-row>
          <el-col :span="8">品牌名称</el-col>
          <el-col :span="16">小米顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶</el-col>
          <el-col>{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="8">描述</el-col>
          <el-col :span="16">小米顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶</el-col>
          <el-col>{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="8">价格</el-col>
          <el-col :span="16">49999</el-col>
          <el-col>{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="8">平台属性</el-col>
          <el-col :span="16">
            <el-tag
              style="margin: 5px"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">销售属性</el-col>
          <el-col :span="16">
            <el-tag v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">商品图片</el-col>
          <el-col :span="16">
            <!-- 轮播图/走马灯 -->
            <el-carousel :interval="4000" type="card" height="200px">
              <!-- <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id"> -->
              <el-carousel-item v-for="item in 6" :key="item">
                <!-- <img :src="skuInfo.imgUrl" alt=""> 根据vue开发工具来确定数据 -->
                <img
                  src="https://img0.baidu.com/it/u=3028529463,3565211658&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1145"
                  alt=""
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>

  <el-button
    type="primary"
    size="small"
    icon="InfoFilled"
    title="查看已有商品详情"
    @click="findSku"
  ></el-button>
</template>

<script lang="ts" setup>
import {
  reqCancelSale,
  reqDeletaSku,
  reqSaleSku,
  reqSkuInfo,
  reqSkuList,
} from '@/api/product/sku'
import type { SkuInfoData, SkuResponseData } from '@/api/product/sku/type'
import type { SkuData } from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'
import { id } from 'element-plus/es/locales.mjs'
import { ref, onMounted } from 'vue'
let pageNo = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(100)
// 控制抽屉的显示与隐藏
let drawer = ref<boolean>(false)
//存储已有的sku数据
let skuArr = ref<SkuData[]>([])
//存储sku商品的详细信息
let skuInfo = ref<any>({})
//页面一旦挂载就发送请求获取数据
onMounted(() => {
  getHasSku()
})
//发送请求获取已有sku
async function getHasSku(pager = 1) {
  //发送请求后跳转的页码,没有pageNo信息默认回到第一页
  pageNo.value = pager
  let result: any = (await reqSkuList(
    pageNo.value,
    pageSize.value,
  )) as unknown as SkuResponseData
  if (result.code === 200) {
    total.value = result.data.total
    skuArr.value = result.data.records
  }
}
//分页器数据条数的下拉框改变的事件回调
function handler(pageSizes: number) {
  //默认回到第一页
  getHasSku()
}
//上架/下架方法的回调
async function updateSale(row: SkuData) {
  //如果isSale==1说明商品是上架状态，点击切换为下架状态
  if (row.isSale == 1) {
    // 下架操作
    await reqCancelSale(row.id as number)
    ElMessage({
      type: 'success',
      message: '下架成功',
    })
    //再次发请求获取更新后的sku
    getHasSku(pageNo.value)
  } else {
    await reqSaleSku(row.id as number)
    ElMessage({
      type: 'success',
      message: '上架成功',
    })
    getHasSku(pageNo.value)
  }
}
//修改sku
function updateSku() {
  ElMessage({
    type: 'success',
    message: '开发中~',
  })
}
//查看商品详情回调
async function findSku(row: SkuData) {
  drawer.value = true
  //发请求获取详情
  let result: any = (await reqSkuInfo(
    row.id as number,
  )) as unknown as SkuInfoData
  //存储已有的sku
  skuInfo.value = result.data
}
//删除sku的事件回调
async function removeSku(id: number) {
  let result: any = await reqDeletaSku(id)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    //获取删除之后的商品信息
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
